namespace ghost
{
    export class InputDialog extends UIBase
    {
        private static _instance:InputDialog;      
        /**输入框显示
         * @param pr 父级
         * @param data \{tip:输入提示,content:输入框默认内容\}
         * @param commit 回调(result:string)=>{}
         * @param restrict 正则字符串限制输入内容
         * @param maxChars 最大输入字符
         * */  
        public static show(pr:Laya.Sprite=null,data:any=null,commit:(data:any)=>void=null,restrict:string=null,maxChars:number=999999):void
		{
            if(InputDialog._instance == null)            
                InputDialog._instance = new InputDialog;
            InputDialog._instance.show(pr, data, commit, restrict, maxChars);
        }
        public lab:Laya.Text;
        public input:Laya.Input;
        constructor()
        {
            super();
            let s= this;
            s.title = "输入框";
            s.lab = new Laya.Text;
            s.lab.color = "#000000";
            s.lab.fontSize = 24;
            s.lab.width = 150;
            s.lab.height = 30;
            s.lab.text = "输入地图ID：";
            s.lab.align = "right";
            s.lab.x = 30;
            s.lab.y = 250;
            s.addChild(s.lab);
            s.input = new Laya.Input;
            s.input.fontSize = 24;
            s.input.color = "#000000";
            s.input.width = 200;
            s.input.height = 30;
            s.input.x = s.lab.width + s.lab.x + 5;
            s.input.y = s.lab.y - 2;
            s.addChild(s.input);            
            s.input.on(Laya.Event.FOCUS, s, s.inputFocus);
            s.input.on(Laya.Event.BLUR, s, s.inputBlur);
        }
        public show(pr:Laya.Sprite=null,data:any=null,commit:(data:any)=>void=null,restrict:string=null,maxChars:number=999999):void
		{
            super.show(pr, data, commit);
            let s= this;
            if(!(maxChars > 0))
                maxChars = 999999;
            s.input.restrict = restrict;
            s.input.maxChars = maxChars;            
        }
        private inputFocus():void
        {            
            GYKeyboard.getInstance().inputFocus = true;
        }
        private inputBlur():void
        {
            GYKeyboard.getInstance().inputFocus = false;
        }
        public commit():void
        {
            let s =this;
            if(s._commitFunc)
            {
                s._commitFunc(s.input.text);
                s.hide();
            }                
        }
        public setData(data:any):void
        {
            if(data)            
            {
                let s= this;
                s.lab.text = data.tip!=null?data.tip:"请输入内容：";
                s.input.text = data.content?data.content:"";
                s.input.x = Math.max(s.lab.width,s.lab.textWidth) + s.lab.x + 5;
            }
        }
    }
}